/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Solitare;

/**
 *
 * @author User
 */
/*Simple Solitare Card Game in Java
 Written by Tim Budd, Oregon State University, 1996
 */

import java.awt.*;
import java.util.Random;
import java.awt.event.*;
import java.util.Enumeration;

public class Solitare {
	static public DeckPile deckPile;
	static public DiscardPile discardPile;
	static public TablePile tableau[];
	static public SuitPile suitPile[];
	static public CardPile allPiles[];
	static public boolean selected;
	private Frame window;
	static public int key;
	static public int cur;

	static public void main(String[] args) {
		Solitare world = new Solitare();
	}

	public Solitare() {
		window = new SolitareFrame();
		init();
		window.addWindowListener(new WindowAdapter(){
		      public void windowClosing(WindowEvent we){
		        System.exit(0);
		      }
		    });
		window.show();
	}

	public void init() {
		// first allocate the arrays
		selected=false;
		key=-1;
		allPiles = new CardPile[13];
		suitPile = new SuitPile[4];
		tableau = new TablePile[7];
		// then fill them in
		allPiles[0] = deckPile = new DeckPile(345, 30);
		allPiles[1] = discardPile = new DiscardPile(268, 30);
		for (int i = 0; i < 4; i++)
			allPiles[2 + i] = suitPile[i] = new SuitPile(15 + (Card.width + 10)
					* i, 30);
		for (int i = 0; i < 7; i++)
			allPiles[6 + i] = tableau[i] = new TablePile(15 + (Card.width + 5)
					* i, Card.height + 30, i + 1);
	}

	private class SolitareFrame extends Frame {

		private class RestartButtonListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				init();
				window.repaint();
			}
		}

		private void hasten() {
				for(int i=6;i<13;i++)
				{
					if(!allPiles[i].isEmpty() && allPiles[i].top().faceUp())
					{
					selected=true;
					key=i;
					allPiles[i].peaks=1;
					for(int j=2;j<6;j++)
						{
						cur=j;
						allPiles[j].select(0,0);
						}
					}
					selected=false;
				}
				if(!allPiles[1].isEmpty() && allPiles[1].top().faceUp())
				{
					Solitare.selected=true;
					key=1;
					allPiles[0].peaks=1;
					for(int j=2;j<6;j++)
						{
						cur=j;
						allPiles[j].select(0,0);
						}
					selected=false;
				}
				for(int j=6;j<13;j++) if(!allPiles[j].isEmpty())
				{
				if(!allPiles[j].top().faceUp()) allPiles[j].top().flip();
				}
				repaint();
			}



		private class MouseKeeper extends MouseAdapter {

			public void mousePressed(MouseEvent e) {
				int x = e.getX();
				int y = e.getY();
				for (int i = 0; i < 13; i++)
					if (allPiles[i].includes(x, y)) {
						if(!selected && (i<2 || i>5))
						{
						allPiles[i].select(x,y);
						if(!allPiles[i].thePile.empty())
						{
						key=i;
						if(i!=0) selected=true;
						repaint();
						}

					} else
					{
						cur=i;
						allPiles[i].select(x, y);
						if(cur==key) hasten();
						selected=false;
						repaint();
					}
					}
						for(int j=6;j<13;j++) if(!allPiles[j].isEmpty())
							{
							if(!allPiles[j].top().faceUp()) allPiles[j].top().flip();
							}
						repaint();
						for(int k=2;k<6;k++)
						{
							if(allPiles[k].isEmpty()) break;
							if(allPiles[k].top().rank()!=12) break;
							if(k==5) System.out.println("You are a winner!!!");
						}
						repaint();


			}
		}

		public SolitareFrame() {
			setSize(600, 700);
			setTitle("Solitaire Game");
			addMouseListener(new MouseKeeper());
			Button restartButton = new Button("New Game");
			restartButton.addActionListener(new RestartButtonListener());
			//Button hastenButton = new Button("Hasten");
			//hastenButton.addActionListener(new HastenButtonListener());
			add("South", restartButton);
			//add("East", hastenButton);
		}

		public void paint(Graphics g) {
			int count=0;
			int r=0;
			Card unique=null;
			Card teemp[]= new Card[100];
			if(selected && key>5)
			{
			r=allPiles[key].thePile.size();
			allPiles[key].thePile.toArray(teemp);
			count=allPiles[key].peaks;
			for(int i=0;i<count;i++)teemp[i]=(Card)allPiles[key].thePile.pop();
			}
			if(selected && key==1) unique=(Card)allPiles[key].thePile.pop();
			for (int i = 0; i < 13; i++)
				 allPiles[i].display(g);
			if(selected)
				{
				if(key>5)
					{
					for(int i=count-1;i>=0;i--)
					teemp[i].draw(g, allPiles[key].x, allPiles[key].y+35*(r-i)+40);
					for(int j=count-1;j>=0;j--) allPiles[key].thePile.push(teemp[j]);
					}
				else
					{
					unique.draw(g, allPiles[key].x, allPiles[key].y+40);
					allPiles[key].thePile.push(unique);
					}
				}
		}
	}
}
